﻿Imports Q7P1TTS.Lib
Imports ComponentFactory.Krypton.Toolkit

Public Class CorrectiveActionForm

    Dim fb As New Complaint()
    Dim act As New Action()
    Public Sub New(ByVal ID As Integer)
        InitializeComponent()
        fb = ComplaintController.load(ID)
        If fb.Investigations.Count > 0 Then
            act = fb.Actions(0)
        End If
    End Sub

    Private Sub CorrectiveActionForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblFbRefNo.Text = fb.RefNumber
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        act.ActionDetail = txtActionDetail.Text.Trim()
        act.ComplaintID = fb.ID
        act.CreateDate = ConvertHelper.ToUnixTimestamp(DateTime.Now)
        Dim emp = SessionHelper.Sessions("USER_INFO")
        If TypeOf emp Is Employee Then
            Dim empl As Employee = emp
            act.CreateBy = empl.ID
        End If
        Try
            If ActionController.save(act) > 0 Then
                fb.Status = ComplaintStatus.Closed
                ComplaintController.save(fb)
                If MessageBox.Show("Your corrective action detail has been successfully saved.") = DialogResult.OK Then
                    Dim mainForm As MainForm = Me.FindForm()
                    mainForm.GoToForm(New FeedBackForm(fb.ID), "Update Complaint: " & fb.RefNumber)
                End If
            Else
                MessageBox.Show("There is an unknown error found when saving data. Please try again later.")
            End If
        Catch ex As Exception
            MessageBox.Show("There is an unknown error found when saving data. Please try again later.")
        End Try
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        For Each c As Control In KryptonPanel1.Controls
            If TypeOf c Is KryptonTextBox Then
                Dim txt As KryptonTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonRichTextBox Then
                Dim txt As KryptonRichTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonComboBox Then
                Dim cbo As KryptonComboBox = c
                cbo.SelectedIndex = 0
            End If
        Next
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.GoToForm(New FeedBackForm(fb.ID), "Update Complaint: " & fb.RefNumber)
    End Sub
End Class
